package top.marken.onlineclass.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.marken.onlineclass.config.CacheKeyManager;
import top.marken.onlineclass.dao.VideoMapper;
import top.marken.onlineclass.model.entity.Video;
import top.marken.onlineclass.model.entity.VideoBanner;
import top.marken.onlineclass.service.VideoService;
import top.marken.onlineclass.util.BaseCache;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private BaseCache baseCache;

    @Override
    public List<Video> listVideo() {
        try {
            Object cacheObj = baseCache.getTenMinuteCache().get(CacheKeyManager.VIDEO_LIST, () -> videoMapper.findAll());
            if (cacheObj instanceof List) {
                return (List<Video>) cacheObj;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<VideoBanner> listVideoBanner() {
        try {
            Object cacheObj = baseCache.getTenMinuteCache().get(CacheKeyManager.VIDEO_BANNER, () -> videoMapper.findAllVideoBanner());
            if (cacheObj instanceof List) {
                return (List<VideoBanner>) cacheObj;
            }
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Video findDetailById(int videoId) {
        try {
            Object cacheObj = baseCache.getOneHourCache().get(CacheKeyManager.getVideoDetailKey(videoId), () -> videoMapper.findDetailById(videoId));
            if (cacheObj instanceof Video) {
                return (Video) cacheObj;
            }
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }
}
